//* 收藏取消收藏
import { good_favlist } from "../../tools/api";
import { good_unfav } from "../../tools/api";
import { Toast } from 'antd-mobile'


// 添加收藏到数组
export function push_arr( params ) {
    return { type:'ADD' , params }
}


// 删除数组中的某个元素
export function delete_arr( params ) {
    return { type:'CLEARCALLECT' , params }
}


//* 拿到收藏列表的数据 传入到收藏的数组里面
export function wang_callect_push( params ) {
    return function ( dispatch ) {
        const id = JSON.stringify(localStorage.getItem('userinfo')).id
        good_favlist( { userid : id } ).then(
            response => { 
                dispatch( push_arr( response.data.list ) )
            }
        )
    }
}

//* 点击取消收藏
export function cancleCallect( params ) {
    return function ( dispatch ) {
        const id = JSON.stringify(localStorage.getItem('userinfo')).id
        good_unfav( { userid : id ,goodid : params.id} ).then (
            response => {
                if( response.data.code == 200 ) {
                    dispatch( delete_arr( params)) 
                    // Toast.show({ icon: 'success', content: '取消收藏成功' })
                } else {
                    Toast.show({ content: '网络出错啦！' })
                }
                // this.setState({})
            }
        )
    }
}